/**
 * Created by gongxun on 17/1/18.
 */

var GsbdxTablePlayer = GameBaseItem.extend({
    mSeat : null,
    Panel_anim : null,
    Image_head : null,
    Text_nick : null,
    Text_score : null,
    Image_ready : null,
    Image_host : null,
    Image_bet : null,
    Text_offline : null,
    Panel_chat : null,
    //time
    Panel_time : null,
    mTimeLeft : null,
    mTimeInt : null,
    //null
    Image_playerNull : null,
    ctor: function (seat) {
        this._super();
        if (seat == 0){
            this.loadCocostudio("res/ccs/GsbdxTablePlayerMy.json");
            this.Panel_time = mo.seekWidgetByName(this.ccsNode, "Panel_time");
            this.Panel_time.setVisible(false);
        }else{
            this.loadCocostudio("res/ccs/GsbdxTablePlayer.json");
            this.Image_playerNull = mo.seekWidgetByName(this.ccsNode, "Image_playerNull");
        }
        mo.gameHelper.fixIosText(this.ccsNode);
        this.mSeat = seat;

        this.Panel_anim = mo.seekWidgetByName(this.ccsNode, "Panel_anim");
        this.Image_head = mo.seekWidgetByName(this.ccsNode, "Image_head");
        this.Text_nick = mo.seekWidgetByName(this.ccsNode, "Text_nick");
        this.Text_score = mo.seekWidgetByName(this.ccsNode, "Text_score");
        this.Image_ready = mo.seekWidgetByName(this.ccsNode, "Image_ready");
        this.Image_host = mo.seekWidgetByName(this.ccsNode, "Image_host");
        this.Image_bet = mo.seekWidgetByName(this.ccsNode, "Image_bet");
        this.Text_offline = mo.seekWidgetByName(this.ccsNode, "Text_offline");
        var Panel_chat = mo.seekWidgetByName(this.ccsNode, "Panel_chat");
        Panel_chat.setVisible(false);
        var Panel_chatFan = mo.seekWidgetByName(this.ccsNode, "Panel_chatFan");
        Panel_chatFan.setVisible(false);
        if (seat == 1 || seat == 2){
            this.Panel_chat = Panel_chatFan;
        }else{
            this.Panel_chat = Panel_chat;
        }

        this.Panel_anim.setVisible(false);
        this.Image_ready.setVisible(false);
        this.Image_host.setVisible(false);
        this.Image_bet.setVisible(false);
        this.Text_offline.setVisible(false);
        this.Panel_chat.setVisible(false);

        mo.seekWidgetByName(this.ccsNode, "Image_player").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.gsbdxTable.mPlayerType == 0){
                    return;
                }

                var index = seat;
                var logicSeat = mo.gsbdxTableStatus.getLogicForView(index, mo.gsbdxTableStatus.mTableStatus.MySeat - 1) + 1;
                var seatUser = mo.gsbdxTableStatus.mTableStatus["SeatUser" + logicSeat];
                if (seatUser != null){
                    mo.gsbdxTable.mGsbdxTableUILayer.mGsbdxTablePlayerInfoLayer.showPlayerInfo(index, seatUser.user_info, mo.gsbdxTable.mGsbdxTableUILayer.mPlayerPoss[index], logicSeat - 1);
                }
            }
        }, this);
    },

    showAddScoreAnim : function(score, delayTime){
        var that = this;
        this.scheduleOnce(function(dt){
            this.Panel_anim.setVisible(true);

            var Image_animBg0 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg0");
            var Image_animBg1 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg1");
            var Image_animBg2 = mo.seekWidgetByName(this.Panel_anim, "Image_animBg2");
            var BitmapFontLabel_add = mo.seekWidgetByName(this.Panel_anim, "BitmapFontLabel_add");
            var BitmapFontLabel_sub = mo.seekWidgetByName(this.Panel_anim, "BitmapFontLabel_sub");

            Image_animBg0.setVisible(false);
            Image_animBg1.setVisible(false);
            Image_animBg2.setVisible(false);
            BitmapFontLabel_add.setVisible(false);
            BitmapFontLabel_sub.setVisible(false);

            if (score > 0){
                var text = score >= 0 ? BitmapFontLabel_add : BitmapFontLabel_sub;

                //分数
                text.setVisible(true);
                text.setOpacity(100);
                text.setPosition(cc.p(0, 0));
                text.setString("+s" + score);
                var textPoxY = this.mSeat == 0 ? 50 : 80;
                text.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.2, cc.p(0, textPoxY)),
                        cc.fadeIn(0.2)
                    ),
                    cc.callFunc(function(){
                        //改变分数
                        var currentScore = parseInt(that.Text_score.getString());
                        currentScore += score;
                        that.Text_score.setString(currentScore + "");
                    }),
                    cc.delayTime(1),
                    cc.hide()
                ));

                //背景0
                Image_animBg0.setVisible(false);
                Image_animBg0.runAction(cc.sequence(
                    cc.delayTime(0.2),
                    cc.show(),
                    cc.delayTime(0.4),
                    cc.hide()
                ));

                //背景0
                Image_animBg1.setVisible(false);
                Image_animBg1.setScale(1.0);
                Image_animBg1.setOpacity(255);
                Image_animBg1.runAction(cc.sequence(
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.4, 1.2),
                        cc.fadeOut(0.4)
                    )
                ));

                //背景0
                Image_animBg2.setVisible(false);
                Image_animBg2.setScale(1.0);
                Image_animBg2.setOpacity(255);
                Image_animBg2.runAction(cc.sequence(
                    cc.delayTime(0.3),
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.4, 1.4),
                        cc.fadeOut(0.4)
                    )
                ));
            }else{
                var text = score >= 0 ? BitmapFontLabel_add : BitmapFontLabel_sub;

                //分数
                text.setVisible(true);
                text.setOpacity(100);
                text.setPosition(cc.p(0, 0));
                text.setString("" + score);
                var textPoxY = this.mSeat == 0 ? 50 : 80;
                text.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(0.2, cc.p(0, textPoxY)),
                        cc.fadeIn(0.2)
                    ),
                    cc.callFunc(function(){
                        //改变分数
                        var currentScore = parseInt(that.Text_score.getString());
                        currentScore += score;
                        that.Text_score.setString(currentScore + "");
                    }),
                    cc.delayTime(1),
                    cc.hide()
                ));
            }
        }, delayTime);
    },

    setPlayerInfo : function(info){
        if (info != null){
            //昵称
            mo.gameHelper.setTextString(this.Text_nick, mo.gameHelper.getUserNickFilter(info.user_info.nickname));

            //头像
            mo.gameHelper.loadImageForUrl(this.Image_head, info.user_info.faceUrl);

            //分数
            this.Text_score.stopAllActions();
            if (mo.gsbdxTableStatus.mTableStatus.Step == 0){
                if (this.Text_score.getString() == "-999"){
                    mo.gameHelper.setTextString(this.Text_score, info.Score + "");
                }else{
                    this.Text_score.runAction(cc.sequence(
                        cc.delayTime(5),
                        cc.callFunc(function(){
                            mo.gameHelper.setTextString(this, info.Score + "");
                        }, this.Text_score)
                    ));
                }
            }else{
                mo.gameHelper.setTextString(this.Text_score, info.Score + "");
            }

            //准备状态
            if (mo.gsbdxTableStatus.mTableStatus.Step == 0 && info.State == 1){
                this.Image_ready.setVisible(true);
            }else{
                this.Image_ready.setVisible(false);
            }

            //庄家
            // if (mo.gsbdxTableStatus.mTableStatus.Step != 0 && info.IsHost){
            if (info.IsHost){
                this.Image_host.setVisible(true);
            }else{
                this.Image_host.setVisible(false);
            }

            //加倍数
            // if ((mo.gsbdxTableStatus.mTableStatus.Step == 2 || mo.gsbdxTableStatus.mTableStatus.Step == 3) && info.AddRate !== 0){
            if (info.AddRate !== 0 && !info.IsHost){
                this.Image_bet.setVisible(true);
                var beiNum = info.AddRate * mo.gsbdxTable.mGameConfig.basepoint;
                mo.gameHelper.loadTextureImage(this.Image_bet, "res/ccs/gsbdx/btn/bet_" + beiNum + ".png")
            }else{
                this.Image_bet.setVisible(false);
            }
        }

        if (this.mSeat != 0){
            mo.seekWidgetByName(this.ccsNode, "Image_player").setVisible(info != null);
            this.Image_playerNull.setVisible(info == null);
        }
    },

    setOnlineStatus : function(isOffline){
        this.Text_offline.setVisible(isOffline);
    },

    setStartTime : function(){
        this.Panel_time.setVisible(true);
        this.mTimeLeft = 10.0;
        this.schedule(this.ontUpdateTimeLeft);
    },

    setCloseTime : function(){
        this.Panel_time.setVisible(false);
        this.unschedule(this.ontUpdateTimeLeft);
    },

    //倒计时
    ontUpdateTimeLeft : function(dt){
        this.mTimeLeft -= dt;
        if (this.mTimeLeft <= 0){
            this.mTimeLeft = 0;
            //发送结束消息
            mo.gsbdxTableStatus.sendKaiPai();

            //播放音乐
            mo.soundHelper.playGsbdxTimeOut();

            this.setCloseTime();
        }
        var LoadingBar_time = mo.seekWidgetByName(this.Panel_time, "LoadingBar_time");
        var percent = this.mTimeLeft / 10.0;
        LoadingBar_time.setPercent(percent * 100);

        var timeInt = parseInt(this.mTimeLeft);
        if (timeInt < 4 && timeInt != this.mTimeInt){
             mo.soundHelper.playGsbdxTimeRun();
        }

        this.mTimeInt = timeInt;
    }
});